package com.glls.search.controller;

import com.glls.common.R;
import com.glls.common.entity.Goods;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/search")
@Slf4j
public class SearchController {



    @Value("${server.port}")
    private String port;


    @RequestMapping("/findGoodsById")
    public Goods search(@RequestParam("id") Integer id) throws InterruptedException {

        log.info("当前端口号:{}",port);

        Goods goods = new Goods(id, "手机"+port, 1000.0);


        //int i = 5/0;

        //TimeUnit.SECONDS.sleep(5);
        //try {
        //    TimeUnit.SECONDS.sleep(3);
        //} catch (InterruptedException e) {
        //    throw new RuntimeException(e);
        //}

        return goods;

    }




    @Value("${common.url}")
    private String url;
    @RequestMapping("/testConfig3")
    public String testConfig3(){
        //测试的是  命名空间 和 分组
        return url;
    }




    //作业-------开始
    @PostMapping("/save")
    public Map save(@RequestBody Goods goods){

        System.out.println("调用了搜素服务的保存接口");
        System.out.println(goods);
        return new HashMap(){{
            put("isSuccess",true);
            put("msg","save success");
        }};
    }

    @PostMapping(value = "/update")
    public Map update(@RequestBody Goods goods) {

        System.out.println(goods);
        return new HashMap(){{
            put("isSuccess",true);
            put("msg","update success");
        }};

    }



    @GetMapping(value = "/delete")
    public Map deleteById(@RequestParam(value = "id") Integer id) {
        System.out.println("删除id为"+id+"的积分信息");
        return new HashMap(){{
            put("isSuccess",true);
            put("msg","delete success");
        }};

    }

    @GetMapping(value = "/{id}")
    public Goods findJifenById(@PathVariable(value = "id") Long id) {
        System.out.println("已经查询到"+id+"积分数据");
        return new Goods(1, "笔记本电脑",66.66);
    }


    @GetMapping(value = "/search")
    public Goods search(Integer id,String name) {
        System.out.println("uid:"+id+"type:"+name);




        return new Goods(id, name,66.66);
    }
    //作业----------结束

    @PostMapping(value = "/searchByEntity")
    public R searchGoodsList(@RequestBody  Goods goods, HttpServletRequest request) {

        String authorization = request.getHeader("Authorization");

        log.info("Authorization:{}",authorization);


        String foo = request.getHeader("Foo");
        log.info("foo:{}",foo);


        System.out.println(goods);


        //int i = 5/0;





        List<Goods> goodsList = new ArrayList<>();
        goodsList.add(new Goods(110,"手机",88.8));
        goodsList.add(new Goods(111,"电脑",66.6));

        return  R.ok().data("goodsList",goodsList);
    }





}
